package Dao;

import DataClass.Teacher;
import Util.JBDCUtil;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class TeacherDao {

    private static PreparedStatement getStatement = null;
    private static PreparedStatement updateStatement = null;

    /**
     * 通过id获取教师信息
     * @param Tno 教师id
     * @return 返回一个教师信息你
     * @throws SQLException 数据库错误
     */
    public static Teacher getTeacher(String Tno) throws SQLException {
        Teacher user = new Teacher();

        ResultSet data;
        try {
            data = TeacherDao.getTeacherList(Tno);
        } catch (SQLException e) {
            System.out.println("教师个人信息查询失败");
            throw new SQLException();
        }

        while (data.next()) {
            String TCName = data.getString("TCName");
            String TSex = data.getString("TSex");
            String JTno = data.getString("JTno");
            String TQualification = data.getString("TQualification");
            String TTno = data.getString("TTno");
            String TDuties = data.getString("TDuties");
            String TSituation = data.getString("TSituation");
            String TEducation = data.getString("TEducation");
            String TFirstDiscipline = data.getString("TFirstDiscipline");
            String TSecondDiscipline = data.getString("TSceondDiscipline");
            String TProfessionalAffiliations = data.getString("TProfessionalAffiliations");
            String TAdminPositionsCollege = data.getString("TAdminPositionsCollege");
            String TAcademicPositionsCollege = data.getString("TAcademicPositionsCollege");
            String TRemark = data.getString("TRemark");
            String TPassword = data.getString("TPassword");

            user = new Teacher(Tno, TCName, TSex, JTno, TQualification, TTno,
                    TDuties, TSituation, TEducation, TFirstDiscipline,
                    TSecondDiscipline, TProfessionalAffiliations, TAdminPositionsCollege,
                    TAcademicPositionsCollege, TRemark,TPassword);
        }

        // 释放资源
        data.close();

        return user;
    }


    /**
     * 获取教师用户的信息
     * @return 返回教师信息
     * @throws SQLException 数据库错误
     */
    public static ResultSet getTeacherList(String Tno) throws SQLException{
        if(getStatement == null)
        {
            String sql = "select * from Teacher WHERE Tno = ?;";
            // 设置statement对象，解析SQL语句，并且保存，节省重复语句解析开销
            getStatement = JBDCUtil.getPreparedStatement(sql);
        }
        getStatement.setString(1, Tno);
        ResultSet result = getStatement.executeQuery();
        JBDCUtil.commit();
        return result;
    }

    /**
     * 教师修改个人密码
     * @param  Tno 教师工号
     * @param  TPassword 教师密码
     * @throws SQLException 数据库异常
     */
    public static void updatePassword(String Tno,String TPassword) throws SQLException{
        if(updateStatement == null)
        {
            String sql = "UPDATE Teacher SET " +
                "TPassword = ? " +
                "WHERE Tno = ? ;" +
                "UPDATE Registered_users SET " +
                "RU_password = ?" +
                "WHERE RU_no = ?;";
            updateStatement = JBDCUtil.getPreparedStatement(sql);
        }

        // 设置参数，注意索引从1开始
        updateStatement.setString(1, TPassword);
        updateStatement.setString(2, Tno);    // 最后设置WHERE子句中的条件
        updateStatement.setString(3, TPassword);
        updateStatement.setString(4, Tno);    // 最后设置WHERE子句中的条件

        // 执行更新操作
        int rowsAffected = updateStatement.executeUpdate();
        JBDCUtil.commit();

        // 处理更新结果
        if (rowsAffected > 0) {
            System.out.println("更新成功。影响的行数：" + rowsAffected);
        } else {
            System.out.println("没有进行更新操作或没有行被影响。");
        }
    }
}